package domain

import (
	"github.com/lab-online/internal/lab/entity"
	"github.com/lab-online/internal/shared/code"
	"github.com/lab-online/internal/shared/valueobject"
)

func (d *Domain) getExpectedReport(
	lab entity.LabEntity, rejectStatus uint,
) (valueobject.ReportVO, error) {
	report, dbErr := d.report.GetReportByStudentAndClassIDAndExperimentID(
		lab.GetStudent(), lab.GetClassID(), lab.GetExperimentID(),
	)

	switch {
	case dbErr != nil:
		return nil, code.ErrorPicker(dbErr, code.DatabaseReportNotFound, nil)
	default:
		if err := handleStatusError(report.GetStatus(), rejectStatus); err != nil {
			return nil, err
		}

		return report, nil
	}
}

func (d *Domain) updateReport(lab entity.LabEntity) (valueobject.ReportVO, error) {
	reportVO := &valueobject.Report{
		ID:      lab.GetID(),
		Status:  lab.GetStatus(),
		Content: lab.GetContent(),
	}

	if reportVO.ID == 0 {
		reportVO.ClassID = lab.GetClassID()
		reportVO.ExperimentID = lab.GetExperimentID()
		reportVO.Student = lab.GetStudent()
	}

	return d.report.SaveReport(reportVO)
}
